<script name="CallbackBind" lang="ts" setup>
import { QQUserInfo, QQUserInfoRes } from '@/types/user'
import { ref } from 'vue'
import { mobileRule, codeRule } from '@/utils/validate'
import { useForm, useField } from 'vee-validate'
import useStore from '@/store'
import Message from '@/components/message'
import { useCountDown } from '@/utils/hooks'
import router from '@/router'
const { user } = useStore()
const props = defineProps({
  openId: {
    type: String,
    required: true,
  },
})
// 获取QQ信息
const info = ref({} as QQUserInfo)
if (QC.Login.check()) {
  QC.api('get_user_info').success((res: QQUserInfoRes) => {
    info.value = res.data
  })
}
// 表单属性校验规则
const { validate: validateForm } = useForm({
  validationSchema: {
    mobile: mobileRule,
    code: codeRule,
  },
})
const {
  value: mobile,
  errorMessage: mobileErrMsg,
  validate,
} = useField<string>('mobile')
const { value: code, errorMessage: codeErrMsg } = useField<string>('code')
const mobileRef = ref()
const { time, start } = useCountDown(60)
// 发送验证码
const sendCode = async () => {
  if (time.value > 0) return
  // 1. 手机号的格式校验
  const { valid } = await validate()
  if (!valid) return mobileRef.value.focus()
  // 2. 格式校验通过后 发送获取验证码的请求
  await user.QQLoginCode(mobile.value)
  // 3. 提示
  Message.success('验证码已发送')
  // 4. 开启倒计时效果
  start()
}
// 绑定QQ+手机号
const handleSubmit = async () => {
  // 1. 预校验
  const { valid } = await validateForm()
  if (!valid) return
  // 2.发送绑定的请求
  await user.bindMobileLogin(props.openId, mobile.value, code.value)
  // 3. 提示
  Message.success('绑定手机成功')
  // 4. 去首页
  router.push('/')
}
</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="info.figureurl_qq" alt="" />
      <p>
        Hi，{{ info.nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~
      </p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
          ref="mobileRef"
        />
      </div>
      <div class="error">{{ mobileErrMsg }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="code"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span class="code" @click="sendCode">{{
          time > 0 ? `${time}s后发送` : '发送验证码'
        }}</span>
      </div>
      <div class="error">{{ codeErrMsg }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="handleSubmit">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
